package com.liwang.lanouhn.nvshengo.fragments;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.liwang.lanouhn.nvshengo.Info.ClothespressInfo;
import com.liwang.lanouhn.nvshengo.Info.Info;
import com.liwang.lanouhn.nvshengo.Info.LoveInfo;
import com.liwang.lanouhn.nvshengo.Info.LunBoInfo;
import com.liwang.lanouhn.nvshengo.R;
import com.liwang.lanouhn.nvshengo.activities.LunBoDetailsActivity;
import com.liwang.lanouhn.nvshengo.adapter.ClothesRecyclerviewAdapter;
import com.liwang.lanouhn.nvshengo.adapter.LoveAdapter;
import com.liwang.lanouhn.nvshengo.utils.HttpUtils;
import com.liwang.lanouhn.nvshengo.view.FullyGridLayoutManager;
import com.liwang.lanouhn.nvshengo.view.FullyLinearLayoutManager;
import com.liwang.lanouhn.nvshengo.view.RecyclerImageView;
import com.squareup.picasso.Picasso;
import com.stx.xhb.xbanner.XBanner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by lanouhn on 2016/9/29.
 */
public class ClothespressFragment extends Fragment implements XBanner.XBannerAdapter, View.OnClickListener {
    private ClothespressInfo clothespressInfo;
    private LoveInfo loveInfo;
    private XBanner xBanner;
    private ClothesRecyclerviewAdapter adapter;
    private LoveAdapter loveAdapter;
    private List<LunBoInfo> lunBoInfos = new ArrayList<>();
    private LunBoInfo lunboinfo = new LunBoInfo();
    private Info info = new Info();
    private XBanner xBanners;
    private TextView buying_head, tv_top, tv_bottom,tv_love;
    private TextView buying_title;
    private RecyclerImageView iv_buying;
    private LinearLayout ll_buying;
    private TextView specialprice_head;
    private TextView specialprice_title;
    private RecyclerImageView iv_specialprice;
    private RelativeLayout ll_specialprice;
    private TextView find_head;
    private TextView find_title;
    private RecyclerImageView iv_find;
    private RelativeLayout ll_find;
    private TextView Supervalue_head;
    private TextView Supervalue_title;
    private RecyclerImageView iv_Supervalue;
    private RelativeLayout ll_Supervalue;
    private CircleImageView iv_sea;
    private TextView Sea_title;
    private RelativeLayout rl_Sea;
    private CircleImageView iv_brand;
    private TextView brand_title;
    private RelativeLayout rl_brand;
    private CircleImageView iv_mom;
    private TextView mom_title;
    private RelativeLayout rl_mom;
    private CircleImageView iv_money;
    private TextView money_title;
    private RelativeLayout rl_money;
    private RecyclerView clothes_recycler;
    private XRecyclerView clothes_xrecyclerview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_clothespress, null);
        initView(view);
        getDataWeb();
        initData();
        initEvent();
        return view;
    }

    private void initView(View view) {
        xBanner = (XBanner) view.findViewById(R.id.xBanners);
        xBanners = (XBanner) view.findViewById(R.id.xBanners);
        xBanners.setOnClickListener(this);
        buying_head = (TextView) view.findViewById(R.id.buying_head);
        tv_love= (TextView) view.findViewById(R.id.tv_love);
        tv_top = (TextView) view.findViewById(R.id.tv_top);
        tv_bottom = (TextView) view.findViewById(R.id.tv_bottom);
        buying_title = (TextView) view.findViewById(R.id.buying_title);
        iv_buying = (RecyclerImageView) view.findViewById(R.id.iv_buying);
        ll_buying = (LinearLayout) view.findViewById(R.id.ll_buying);
        ll_buying.setOnClickListener(this);
        specialprice_head = (TextView) view.findViewById(R.id.specialprice_head);
        specialprice_title = (TextView) view.findViewById(R.id.specialprice_title);
        iv_specialprice = (RecyclerImageView) view.findViewById(R.id.iv_specialprice);
        ll_specialprice = (RelativeLayout) view.findViewById(R.id.ll_specialprice);
        ll_specialprice.setOnClickListener(this);
        find_head = (TextView) view.findViewById(R.id.find_head);
        find_title = (TextView) view.findViewById(R.id.find_title);
        iv_find = (RecyclerImageView) view.findViewById(R.id.iv_find);
        ll_find = (RelativeLayout) view.findViewById(R.id.ll_find);
        ll_find.setOnClickListener(this);
        Supervalue_head = (TextView) view.findViewById(R.id.Supervalue_head);
        Supervalue_title = (TextView) view.findViewById(R.id.Supervalue_title);
        iv_Supervalue = (RecyclerImageView) view.findViewById(R.id.iv_Supervalue);
        ll_Supervalue = (RelativeLayout) view.findViewById(R.id.ll_Supervalue);
        ll_Supervalue.setOnClickListener(this);
        iv_sea = (CircleImageView) view.findViewById(R.id.iv_Sea);
        Sea_title = (TextView) view.findViewById(R.id.Sea_title);
        rl_Sea = (RelativeLayout) view.findViewById(R.id.rl_Sea);
        rl_Sea.setOnClickListener(this);
        iv_brand = (CircleImageView) view.findViewById(R.id.iv_brand);
        brand_title = (TextView) view.findViewById(R.id.brand_title);
        rl_brand = (RelativeLayout) view.findViewById(R.id.rl_brand);
        rl_brand.setOnClickListener(this);
        iv_mom = (CircleImageView) view.findViewById(R.id.iv_mom);
        mom_title = (TextView) view.findViewById(R.id.mom_title);
        rl_mom = (RelativeLayout) view.findViewById(R.id.rl_mom);
        rl_mom.setOnClickListener(this);
        iv_money = (CircleImageView) view.findViewById(R.id.iv_money);
        money_title = (TextView) view.findViewById(R.id.money_title);
        rl_money = (RelativeLayout) view.findViewById(R.id.rl_money);
        rl_money.setOnClickListener(this);
        clothes_recycler = (RecyclerView) view.findViewById(R.id.clothes_recycler);
        clothes_recycler.setOnClickListener(this);
        clothes_xrecyclerview = (XRecyclerView) view.findViewById(R.id.clothes_xrecyclerview);
        FullyGridLayoutManager gridLayoutManager =new FullyGridLayoutManager(getActivity(),2);
        clothes_xrecyclerview.setLayoutManager(gridLayoutManager);
    }

    private void getDataWeb() {
        xBanner.setmAdapter(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://112.25.252.33/gw/mwp.darwin.makeup/3/?rnd=iufhh9hgyby5la";
                String path = "data=%7B%0A++%22market%22%3A+%22%22%2C%0A++%22pid%22%3A+%2210181%22%0A%7D";
                try {
                    Map<String, Object> headers = new HashMap<>();
                    headers.put("mw-uid", "142bekg");
                    headers.put("Cache-Control", "no-cache");
                    headers.put("mw-lang", "zh_CN");
                    headers.put("mw-cmd-v", "1476790488");
                    headers.put("mw-nq", "WIFI");
                    headers.put("mw-dinfo", "SM-C7000%4023%401080");
                    headers.put("mw-did2", "e7397c7c-1c90-401a-ba88-1de38d28d21b");
                    headers.put("User-Agent", "Mogujie4Android%2FMAMuc811%2F811 Paros/3.2.13");
                    headers.put("mw-pv", "1.2");
                    headers.put("mw-net", "WIFI");
                    headers.put("mw-did", "358811076973443");
                    headers.put("mw-appkey", "100003");
                    headers.put("mw-fs", "MAMuc811");
                    headers.put("Enable-HTTPDNS", "1");
                    headers.put("Host", "api.mogujie.com");
                    headers.put("mw-t", "1476794777360");
                    headers.put("mw-sign", "210d576583ce009f");
                    headers.put("mw-ttid", "MAMuc811%40mgj_Android_8.1.1_2249");
                    headers.put("mw-sid", "jr8cUI28ZBojlO7IROW33v68KxxlkhkxtJKy9ePPG%2FONaJJDZPw0Gipu05f7ZAqf4TSymSPjkKZeFYhUBokjPg%3D%3D");
                    headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    headers.put("Connection", "Keep-Alive");
                    headers.put("Content-Length", "73");
                    HttpUtils.doPostAsyns(url, path, headers, new HttpUtils.CallBack() {
                        @Override
                        public void onRequestComplete(String result) {
                            Gson gson = new Gson();
                            clothespressInfo = gson.fromJson(result, ClothespressInfo.class);
                            if (null != clothespressInfo.getData()) {
                                handler.sendEmptyMessage(1);
                            } else {
                                handler.sendEmptyMessage(0);
                            }
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://api.mogujie.com/gw/mwp.darwin.multiget/3/?rnd=iugauvuayby5la";
                String path = "data=%7B%0A++%22pids%22%3A+%2213085%2C15679%2C1157%2C1%2C8549%2C11770%2C11751%22%0A%7D";
                try {
                    final Map<String, Object> headers = new HashMap<>();
                    headers.put("mw-uid", "142bekg");
                    headers.put("Cache-Control", "no-cache");
                    headers.put("mw-lang", "zh_CN");
                    headers.put("mw-cmd-v", "1476790488");
                    headers.put("mw-nq", "WIFI");
                    headers.put("mw-dinfo", "SM-C7000%4023%401080");
                    headers.put("mw-did2", "e7397c7c-1c90-401a-ba88-1de38d28d21b");
                    headers.put("User-Agent", "Mogujie4Android%2FMAMuc811%2F811 Paros/3.2.13");
                    headers.put("mw-pv", "1.2");
                    headers.put("mw-net", "WIFI");
                    headers.put("mw-did", "358811076973443");
                    headers.put("mw-appkey", "100003");
                    headers.put("mw-fs", "MAMuc811");
                    headers.put("mw-t", "1476844121694");
                    headers.put("mw-sign", "c03e8eb7ae28fad8");
                    headers.put("mw-ttid", "MAMuc811%40mgj_Android_8.1.1_2249");
                    headers.put("mw-sid", "jr8cUI28ZBojlO7IROW33v68KxxlkhkxtJKy9ePPG%2FONaJJDZPw0Gipu05f7ZAqf4TSymSPjkKZeFYhUBokjPg%3D%3D");
                    headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    headers.put("Host", "api.mogujie.com");
                    headers.put("Connection", "Keep-Alive");
                    headers.put("Content-Length", "86");
                    HttpUtils.doPostAsyns(url, path, headers, new HttpUtils.CallBack() {
                        @Override
                        public void onRequestComplete(String result) {
                            String finalResult = Html.fromHtml(result).toString();
                            try {
                                JSONObject jsonObject = new JSONObject(finalResult);
                                JSONObject object = jsonObject.getJSONObject("data");
                                JSONObject jsonObject1 = object.getJSONObject("1");
                                JSONArray array = jsonObject1.getJSONArray("list");
                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject jsonObject2 = array.getJSONObject(i);
                                    LunBoInfo lunBoInfo = new LunBoInfo();
                                    lunBoInfo.setTitle(jsonObject2.getString("title"));
                                    lunBoInfo.setLink(jsonObject2.getString("link"));
                                    lunBoInfo.setImage(jsonObject2.getString("image"));
                                    lunBoInfos.add(lunBoInfo);
                                }
                                if (lunBoInfos.size() > 0) {
                                    handler.sendEmptyMessage(2);
                                } else {
                                    handler.sendEmptyMessage(0);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
             String url="http://112.25.252.33/gw/mwp.pagani.search/4/?rnd=iufhh9zmyby5la";
              String path="data=%7B%0A++%22cKey%22%3A+%22app-shopping%22%2C%0A++%22_version%22%3A+%221%22%2C%0A++%22pid%22%3A+%229750%22%0A%7D";
                try {
                    Map<String, Object> headers = new HashMap<>();
                    headers.put("mw-uid", "142bekg");
                    headers.put("Cache-Control", "no-cache");
                    headers.put("mw-lang", "zh_CN");
                    headers.put("mw-cmd-v", "1476790488");
                    headers.put("mw-nq", "WIFI");
                    headers.put("mw-dinfo", "SM-C7000%4023%401080");
                    headers.put("mw-did2", "e7397c7c-1c90-401a-ba88-1de38d28d21b");
                    headers.put("User-Agent", "Mogujie4Android%2FMAMuc811%2F811 Paros/3.2.13");
                    headers.put("mw-pv", "1.2");
                    headers.put("mw-net", "WIFI");
                    headers.put("mw-did", "358811076973443");
                    headers.put("mw-appkey", "100003");
                    headers.put("mw-fs", "MAMuc811");
                    headers.put("Enable-HTTPDNS", "1");
                    headers.put("Host", "api.mogujie.com");
                    headers.put("mw-t", "1476794777906");
                    headers.put("mw-sign", "b472035d96f5c49d");
                    headers.put("mw-ttid", "MAMuc811%40mgj_Android_8.1.1_2249");
                    headers.put("mw-sid", "jr8cUI28ZBojlO7IROW33v68KxxlkhkxtJKy9ePPG%2FONaJJDZPw0Gipu05f7ZAqf4TSymSPjkKZeFYhUBokjPg%3D%3D");
                    headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    headers.put("Connection", "Keep-Alive");
                    headers.put("Content-Length", "115");
                    HttpUtils.doPostAsyns(url, path, headers, new HttpUtils.CallBack() {
                        @Override
                        public void onRequestComplete(String result) {
                            Gson gson = new Gson();
                            loveInfo = gson.fromJson(result, LoveInfo.class);
                            if (loveInfo.getData().getWall().getList().size()>0) {
                                handler.sendEmptyMessage(3);
                            } else {
                                handler.sendEmptyMessage(0);
                            }
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "网络请求失败!", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    buying_head.setText(clothespressInfo.getData().getSecPromotions().getList().get(0).getTitle());
                    buying_title.setText(clothespressInfo.getData().getSecPromotions().getList().get(0).getViceTitle());
                    tv_top.setText(clothespressInfo.getData().getSecPromotions().getList().get(0).getItemName());
                    tv_bottom.setText("¥" + clothespressInfo.getData().getSecPromotions().getList().get(0).getItemPrice());
                    Picasso.with(getActivity()).load(clothespressInfo.getData().getSecPromotions().getList().get(0).getImage()).fit().config(Bitmap.Config.RGB_565).into(iv_buying);
                    specialprice_head.setText(clothespressInfo.getData().getSecPromotions().getList().get(1).getTitle());
                    specialprice_title.setText(clothespressInfo.getData().getSecPromotions().getList().get(1).getViceTitle());
                    Picasso.with(getActivity()).load(clothespressInfo.getData().getSecPromotions().getList().get(1).getImage()).fit().config(Bitmap.Config.RGB_565).into(iv_specialprice);
                    find_head.setText(clothespressInfo.getData().getSecPromotions().getList().get(2).getTitle());
                    find_title.setText(clothespressInfo.getData().getSecPromotions().getList().get(2).getViceTitle());
                    Picasso.with(getActivity()).load(clothespressInfo.getData().getSecPromotions().getList().get(2).getImage()).fit().config(Bitmap.Config.RGB_565).into(iv_find);
                    Supervalue_head.setText(clothespressInfo.getData().getSecPromotions().getList().get(3).getTitle());
                    Supervalue_title.setText(clothespressInfo.getData().getSecPromotions().getList().get(3).getViceTitle());
                    Picasso.with(getActivity()).load(clothespressInfo.getData().getSecPromotions().getList().get(3).getImage()).fit().config(Bitmap.Config.RGB_565).into(iv_Supervalue);
                    Sea_title.setText(clothespressInfo.getData().getKeySubarea().getList().get(0).getTitle());
                    Picasso.with(getActivity()).load(clothespressInfo.getData().getKeySubarea().getList().get(0).getImage()).fit().config(Bitmap.Config.RGB_565).into(iv_sea);
                    brand_title.setText(clothespressInfo.getData().getKeySubarea().getList().get(1).getTitle());
                    Picasso.with(getActivity()).load(clothespressInfo.getData().getKeySubarea().getList().get(1).getImage()).fit().config(Bitmap.Config.RGB_565).into(iv_brand);
                    mom_title.setText(clothespressInfo.getData().getKeySubarea().getList().get(2).getTitle());
                    Picasso.with(getActivity()).load(clothespressInfo.getData().getKeySubarea().getList().get(2).getImage()).fit().config(Bitmap.Config.RGB_565).into(iv_mom);
                    money_title.setText(clothespressInfo.getData().getKeySubarea().getList().get(3).getTitle());
                    Picasso.with(getActivity()).load(clothespressInfo.getData().getKeySubarea().getList().get(3).getImage()).fit().config(Bitmap.Config.RGB_565).into(iv_money);
                    adapter = new ClothesRecyclerviewAdapter(getActivity(), clothespressInfo);
                    clothes_recycler.setAdapter(adapter);
                    FullyLinearLayoutManager manager = new FullyLinearLayoutManager(getActivity());
                    clothes_recycler.setLayoutManager(manager);
                    break;
                case 2:
                    xBanner.setData(lunBoInfos);
                    break;
                case 3:
                    tv_love.setText(loveInfo.getData().getWall().getTitle());
                    loveAdapter=new LoveAdapter(getActivity(),loveInfo.getData().getWall().getList());
                    clothes_xrecyclerview.setAdapter(loveAdapter);
                    break;
            }

        }
    };

    @Override
    public void loadBanner(XBanner banner, View view, int position) {
        Picasso.with(getActivity()).load(lunBoInfos.get(position).getImage()).fit().config(Bitmap.Config.RGB_565).into((ImageView) view);
    }

    private void initData() {
        xBanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, int position) {
                lunboinfo = lunBoInfos.get(position);
                info.setTitle(lunboinfo.getTitle());
                info.setUrl(lunboinfo.getLink());
                Intent intent = new Intent(getActivity(), LunBoDetailsActivity.class);
                intent.putExtra("info", info);
                startActivity(intent);
            }
        });
    }

    private void initEvent() {

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_buying:
                Info infos = new Info();
                infos.setTitle(clothespressInfo.getData().getSecPromotions().getList().get(0).getTitle());
                infos.setUrl(clothespressInfo.getData().getSecPromotions().getList().get(0).getLink());
                Intent intent8 = new Intent(getActivity(), LunBoDetailsActivity.class);
                intent8.putExtra("info", infos);
                startActivity(intent8);
                break;
            case R.id.ll_specialprice:
                Info info1 = new Info();
                info1.setTitle(clothespressInfo.getData().getSecPromotions().getList().get(1).getTitle());
                info1.setUrl(clothespressInfo.getData().getSecPromotions().getList().get(1).getLink());
                Intent intent1 = new Intent(getActivity(), LunBoDetailsActivity.class);
                intent1.putExtra("info", info1);
                startActivity(intent1);
                break;
            case R.id.ll_find:
                Info info2 = new Info();
                info2.setTitle(clothespressInfo.getData().getSecPromotions().getList().get(2).getTitle());
                info2.setUrl(clothespressInfo.getData().getSecPromotions().getList().get(2).getLink());
                Intent intent2 = new Intent(getActivity(), LunBoDetailsActivity.class);
                intent2.putExtra("info", info2);
                startActivity(intent2);
                break;
            case R.id.ll_Supervalue:
                Info info3 = new Info();
                info3.setTitle(clothespressInfo.getData().getSecPromotions().getList().get(3).getTitle());
                info3.setUrl(clothespressInfo.getData().getSecPromotions().getList().get(3).getLink());
                Intent intent3 = new Intent(getActivity(), LunBoDetailsActivity.class);
                intent3.putExtra("info", info3);
                startActivity(intent3);
                break;
            case R.id.rl_Sea:
                Info info4 = new Info();
                info4.setTitle(clothespressInfo.getData().getKeySubarea().getList().get(0).getTitle());
                info4.setUrl(clothespressInfo.getData().getKeySubarea().getList().get(0).getLink());
                Intent intent4 = new Intent(getActivity(), LunBoDetailsActivity.class);
                intent4.putExtra("info", info4);
                startActivity(intent4);

                break;
            case R.id.rl_brand:
                Info info5 = new Info();
                info5.setTitle(clothespressInfo.getData().getKeySubarea().getList().get(1).getTitle());
                info5.setUrl(clothespressInfo.getData().getKeySubarea().getList().get(1).getLink());
                Intent intent5 = new Intent(getActivity(), LunBoDetailsActivity.class);
                intent5.putExtra("info", info5);
                startActivity(intent5);
                break;
            case R.id.rl_mom:
//                Info info6 =new Info();
//                info6.setTitle(clothespressInfo.getData().getKeySubarea().getList().get(2).getTitle());
//                info6.setUrl(clothespressInfo.getData().getKeySubarea().getList().get(2).getLink());
//                Intent intent6 = new Intent(getActivity(), LunBoDetailsActivity.class);
//                intent6.putExtra("info", info6);
//                startActivity(intent6);
                break;
            case R.id.rl_money:
                Info info7 = new Info();
                info7.setTitle(clothespressInfo.getData().getKeySubarea().getList().get(3).getTitle());
                info7.setUrl(clothespressInfo.getData().getKeySubarea().getList().get(3).getLink());
                Intent intent7 = new Intent(getActivity(), LunBoDetailsActivity.class);
                intent7.putExtra("info", info7);
                startActivity(intent7);
                break;

        }

    }
}
